<?php
/**
 * 提醒30 之内将要转正或续签的员工
 */
apf_require_class('Oa_Job_Abstract');
apf_require_class('Bll_Hr_OutInterface');
apf_require_class('Tools_SendMail');

class Oa_Job_Flow_CheckRegularRenewTip extends Oa_Job_Abstract {

    private $writedb = "hroa_master";//写数据库

    public function run($params){
        $time_start = time();
        $this->log("START".date('Ymd')."==开始执行啦！");//方便搜索时找到指定的位置

        $this->hr_bll = new Bll_Hr_OutInterface();
        $this->_today = strtotime(date('Ymd'));
        $_time = $this->_today+30*86400;//合同结束时间等于这个
        $this->_time = date('Ymd',$_time);//30天之后的时间,需要提前一天

        $this->hrlist = $this->_hr_list();
        //转正
        $this->_build_regular_users();
        //续签
        $this->_build_renew_users();

        $time_end = time();
        $this->log('(End Job--'.date('Y-m-d H:i:s').')'.($time_end-$time_start).'seconds');
    }
    private function _hr_list(){
        $sql = "SELECT * FROM role where role_type=5";
        $tmp = $this->get_db_rows($sql,$this->writedb,array());
        //按城市归类
        $data = array();
        $uids = array();
        foreach($tmp as $val){
            if($val['user_id']==6872){
                continue;
            }
            if($val['city_id']!=11 && in_array($val['user_id'],array(6214,4183,5012,5300))){
                continue;
            }
            $data[$val['city_id']][$val['user_id']] = $val['user_id'];
            $uids[]=$val['user_id'];
        }
        $u_sql = "SELECT user_id,user_name,office_mail FROM user where job_status=1 AND user_id IN(".implode(',',$uids).")";
        $datalist = $this->get_db_rows_assoc($u_sql,$this->writedb,array());
        foreach($data as $city=>$list){
            foreach($list as $uid=>$val){
                $data[$city][$uid]=array($datalist[$uid]['office_mail'],$datalist[$uid]['user_name']);
            }
        }
        return $data;
    }
    //转正
    private function _build_regular_users(){
        $sql = "SELECT * FROM user_regular WHERE effect_date=".$this->_time;
        $tmp = $this->get_db_rows_assoc($sql,$this->writedb,array());
        if(empty($tmp)){
            return array();
        }

        foreach($tmp as $val){
            $super_data = $this->_get_user_super($val['user_id']);
            if(empty($super_data)){
                continue;
            }
            $this->log("正在给 {$super_data['name']} 的邮箱 {$super_data['email']} 发送邮件......");
            $mailcontent = $this->_build_mail_template($super_data['name'],$val['user_name'],$val['effect_date'],2);
            $mail_tool = Tools_SendMail::get_instance();
            /*
            $this->hrlist[$val['work_city_id']] =array(
                array('andycao@anjuke.com','xxxxxxx')
            );
            $mail_tool->add_cc($this->hrlist[$val['work_city_id']]);
            $mail_tool->send('lushuizheng@anjuke.com','员工转正提醒',$mailcontent);
            exit;*/

            if(!empty($this->hrlist[$val['work_city_id']])){
                    $mail_tool->add_cc($this->hrlist[$val['work_city_id']]);
            }
            $mail_tool->send($super_data['email'],'员工转正提醒',$mailcontent);
        }
        return $tmp;
    }
    //续签
    private function _build_renew_users(){
        $sql = "SELECT * FROM user_renew WHERE effect_date=".$this->_time;
        $tmp = $this->get_db_rows_assoc($sql,$this->writedb,array());
        if(empty($tmp)){
            return array();
        }
        foreach($tmp as $val){
            $super_data = $this->_get_user_super($val['user_id']);
            if(empty($super_data)){
                continue;
            }
            $this->log("正在给 {$super_data['name']} 的邮箱 {$super_data['email']} 发送邮件......");
            $mailcontent = $this->_build_mail_template($super_data['name'],$val['user_name'],$val['effect_date']);
            $mail_tool = Tools_SendMail::get_instance();
            if(!empty($this->hrlist[$val['work_city_id']])){
                $mail_tool->add_cc($this->hrlist[$val['work_city_id']]);
            }
            $mail_tool->send($super_data['email'],'员工续签提醒',$mailcontent);
        }
        return $tmp;
    }
    //得到主管
    private function _get_user_super($user_id){
        $super_data = $this->hr_bll->get_under_leader($user_id);
        if(empty($super_data)){
            $this->_error_info[]=$user_id." Not Found Super";
        }
        return $super_data;
    }
    private function _build_mail_template($username,$uname,$date,$is_renew=1){
        $type = $is_renew ==1 ? '续签' : '转正';
        $day = date('Y-m-d',strtotime($date));
        $result = "Dear {$username}:<br><br>";
        $result .= "&nbsp;&nbsp;您的下属员工&nbsp;&nbsp;<font color=red><b>{$uname}</b></font>&nbsp;&nbsp;将于{$day}&nbsp;&nbsp;<font color=red><b>{$type}</b></font>。请在待办事项中帮其办理。";
        $result .= "<br><br>以上<br><br>此邮件系统自动发送，请勿回复！<br><br>";
        return $result;
    }
}